package Game;

import java.awt.*;
import java.awt.event.KeyEvent;

public class Player{
    Image img;
    double x,y;
    int speed,width,height;
    int score;
    boolean p1_left,p1_up,p1_right,p1_down;
    boolean p2_left,p2_up,p2_right,p2_down;

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public Player(Image img, double x1, double y1){
        this.img = img;
        this.x = x1;
        this.y = y1;
        this.speed = 10;
        this.score = 1;
        this.width = img.getWidth(null);//获取图像的宽度
        this.height = img.getHeight(null);//获取图像的高度
    }

    public Player(Image img, double x, double y, int speed, int width, int height) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.width = width;
        this.height = height;
    }

    /*返回物体所在的矩形，便于后续检测*/
    public Rectangle getRect(){
        return new Rectangle((int)x,(int)y,width,height);
    }


    public void drawSelf(Graphics g) {
        g.drawImage(img, (int)x, (int)y,null);
        if(p1_left) {
            x-=speed;
        }
        if(p1_right) {
            x+=speed;
        }
        if(p1_up) {
            y-=speed;
        }
        if(p1_down) {
            y+=speed;
        }

        if(p2_left) {
            x-=speed;
        }
        if(p2_right) {
            x+=speed;
        }
        if(p2_up) {
            y-=speed;
        }
        if(p2_down) {
            y+=speed;
        }
    }
    //按下某个键，增加相应的方向
    public void addDirection1(KeyEvent e) {
        switch(e.getKeyCode()) {
            case KeyEvent.VK_A :
                if(x < 0)
                    x =10;
                p1_left = true;
                break;
            case KeyEvent.VK_D :
                if(x > 900)
                    x = 890;
                p1_right = true;
                break;
            case KeyEvent.VK_W :
                if(y < 0)
                    y = 10;
                p1_up =true;
                break;
            case KeyEvent.VK_S :
                if(y > 650)
                    y = 630;
                p1_down = true;
                break;
        }
    }
    //抬起某个键，取消相应的方向
    public void minusDirection1(KeyEvent e) {
        switch(e.getKeyCode()) {
            case KeyEvent.VK_A:
                if(x < 0)
                    x = 10;
                p1_left = false;
                break;
            case KeyEvent.VK_D:
                if(x > 900)
                    x = 890;
                p1_right = false;
                break;
            case KeyEvent.VK_W:
                if(y < 0)
                    y = 10;
                p1_up = false;
                break;
            case KeyEvent.VK_S:
                if(y > 650)
                    y = 630;
                p1_down = false;
                break;
        }
    }

    //按下某个键，增加相应的方向
    public void addDirection2(KeyEvent e) {
        switch(e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                if(x < 0)
                    x = 10;
                p2_left = true;
                break;
            case KeyEvent.VK_RIGHT:
                if(x > 900)
                    x = 890;
                p2_right = true;
                break;
            case KeyEvent.VK_UP :
                if(y < 0)
                    y = 10;
                p2_up =true;
                break;
            case KeyEvent.VK_DOWN :
                if(y > 650)
                    y = 630;
                p2_down = true;
                break;
        }
    }
    //抬起某个键，取消相应的方向
    public void minusDirection2(KeyEvent e) {
        switch(e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                if(x < 0)
                    x = 10;
                p2_left = false;
                break;
            case KeyEvent.VK_RIGHT:
                if(x > 900)
                    x = 890;
                p2_right = false;
                break;
            case KeyEvent.VK_UP:
                if(y < 0)
                    y = 10;
                p2_up = false;
                break;
            case KeyEvent.VK_DOWN:
                if(y > 650)
                    y = 630;
                p2_down = false;
                break;
        }
    }
}
